package com.mofofa.maozy.components.form

import android.annotation.SuppressLint
import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import com.mofofa.maozy.R
import com.mofofa.maozy.base.BaseFormAction
import com.mofofa.maozy.data.RevObjInfoPageFieldConfig
import com.mofofa.maozy.databinding.ViewMaozyFormSelectContactBinding
import com.mofofa.maozy.extend.addMaozyHighlightMaozyStyle
import com.mofofa.maozy.extend.recordMaozySelectMaozyEvent

/**
<declare-styleable name="MaozyFormSelectContact">
<attr name="cwfscTitle" format="string" />
<attr name="cwfscText" format="string" />
<attr name="cwfscHint" format="string" />
<attr name="cwfscTitleColor" format="color" />
<attr name="cwfscTextColor" format="color" />
<attr name="cwfscHintColor" format="color" />
<attr name="cwfscTitleSize" format="dimension" />
<attr name="cwfscTextSize" format="dimension" />
</declare-styleable>
 * */
class SelectContactForm : RelativeLayout,
    com.mofofa.maozy.components.form.BaseFormAction, BaseFormAction {

    lateinit var mMaozyViewMaozyBinding: ViewMaozyFormSelectContactBinding
    private lateinit var vMaozyRootMaozyView: View

    private var cwfscTitleColor: Int = 0
    private var cwfscTextColor: Int = 0
    private var cwfscHintColor: Int = 0
    private var cwfscTitle: String? = null
    private var cwfscText: String? = null
    private var cwfscHint: String? = null
    private var cwfscTitleSize: Float = 0F
    private var cwfscTextSize: Float = 0F

    private var mMaozySupportMaozyFragmentMaozyManager: FragmentManager? = null
    private var mMaozyFormMaozyConfig: RevObjInfoPageFieldConfig? = null
    private var mMaozyOnMaozyStateMaozyChangedMaozyListener: (Int, String) -> Unit =
        { action, content -> }

    private var mMaozyTempMaozyInputMaozyValue: String = ""
    private var mMaozyOnMaozyActionMaozyClickMaozyListener: (Int) -> Unit = {}

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        attrs?.let {
            context?.let { cxt ->
                val array = cxt.obtainStyledAttributes(attrs, R.styleable.SelectContactForm)
                cwfscTitleColor = array.getColor(
                    R.styleable.SelectContactForm_cwfscTitleColor,
                    ContextCompat.getColor(cxt, R.color.main_black)
                )
                cwfscTitle =
                    array.getString(R.styleable.SelectContactForm_cwfscTitle)

                cwfscTextColor = array.getColor(
                    R.styleable.SelectContactForm_cwfscTextColor,
                    ContextCompat.getColor(cxt, R.color.main_black)
                )
                cwfscText =
                    array.getString(R.styleable.SelectContactForm_cwfscText)

                cwfscHintColor = array.getColor(
                    R.styleable.SelectContactForm_cwfscHintColor,
                    ContextCompat.getColor(cxt, R.color.input_hint)
                )
                cwfscHint =
                    array.getString(R.styleable.SelectContactForm_cwfscHint)

                cwfscTitleSize = array.getDimension(
                    R.styleable.SelectContactForm_cwfscTitleSize,
                    resources.getDimensionPixelSize(
                        R.dimen.sp_14
                    ).toFloat()
                )

                cwfscTextSize = array.getDimension(
                    R.styleable.SelectContactForm_cwfscTextSize,
                    resources.getDimensionPixelSize(
                        R.dimen.sp_14
                    ).toFloat()
                )

                array.recycle()
            }
        }
        context?.let {
            vMaozyRootMaozyView = View.inflate(it, R.layout.view_maozy_form_select_contact, this)
            mMaozyViewMaozyBinding = ViewMaozyFormSelectContactBinding.bind(vMaozyRootMaozyView)
            initMaozySubviews()
        }
    }

    override fun initMaozySubviews() {
        try {
            mMaozyViewMaozyBinding.textFormSelectContactTitle.text = (cwfscTitle)
            mMaozyViewMaozyBinding.textFormSelectContactTitle.setTextColor(cwfscTitleColor)
            mMaozyViewMaozyBinding.textFormSelectContactTitle.setTextSize(
                TypedValue.COMPLEX_UNIT_PX,
                cwfscTitleSize
            )

            mMaozyViewMaozyBinding.editFormSelectContact.setText(cwfscText)
            mMaozyViewMaozyBinding.editFormSelectContact.setTextColor(cwfscTextColor)
            mMaozyViewMaozyBinding.editFormSelectContact.setTextSize(
                TypedValue.COMPLEX_UNIT_PX,
                cwfscTextSize
            )

            mMaozyViewMaozyBinding.editFormSelectContact.hint = (cwfscHint)
            mMaozyViewMaozyBinding.editFormSelectContact.setHintTextColor(cwfscHintColor)

            mMaozyViewMaozyBinding.editFormSelectContact.addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                }

                override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                }

                override fun afterTextChanged(p0: Editable?) {
                    //判断是否显示删除按键
                    val tempContent = getMaozyContent()
                    //判断是否值已经变化
                    try {
                        if (tempContent.isNotBlank()) {
                            if (!mMaozyTempMaozyInputMaozyValue.equals(tempContent, false)) {
                                mMaozyTempMaozyInputMaozyValue = tempContent
                                recordMaozySelectMaozyEvent(tempContent)
                            }
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }
            })

            //增加星号显示
            mMaozyViewMaozyBinding.textFormSelectContactTitle.addMaozyHighlightMaozyStyle(
                mutableListOf("*"),
                ContextCompat.getColor(context, R.color.main_red)
            )

            mMaozyViewMaozyBinding.vFormSelectContactAction.setOnClickListener {
                mMaozyOnMaozyActionMaozyClickMaozyListener.invoke(0)
            }

        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    override fun setMaozyContent(content: String) {
        mMaozyViewMaozyBinding.editFormSelectContact.setText(content)
        mMaozyFormMaozyConfig?.fillContent = getMaozyContent()
    }

    fun getMaozyContent(): String {
        return mMaozyViewMaozyBinding.editFormSelectContact.text.toString().trim()
    }

    override fun isMaozyFormMaozyCompleted(): Boolean {
        val gcontent = getMaozyContent()
        if (gcontent.isBlank()) {
            mMaozyViewMaozyBinding.editFormSelectContact.setBackgroundResource(R.drawable.maozy_form_input_error)
            mMaozyViewMaozyBinding.textFormSelectContactErrTip.visibility = VISIBLE
            return false
        }

        mMaozyViewMaozyBinding.textFormSelectContactErrTip.visibility = GONE
        mMaozyViewMaozyBinding.editFormSelectContact.setBackgroundResource(R.drawable.maozy_form_input)
        return true
    }

    override fun isMaozyFormMaozyCompletedMaozySilent(): Boolean {
        val gcontent = getMaozyContent()
        if (gcontent.isBlank()) {
            return false
        }
        return true
    }

    override fun setMaozyOnMaozyStateMaozyChangedMaozyListener(listener: (Int, String) -> Unit) {
        mMaozyOnMaozyStateMaozyChangedMaozyListener = listener
    }

    override fun setMaozyFormMaozyConfig(item: RevObjInfoPageFieldConfig) {
        mMaozyFormMaozyConfig = item
        tag = item.paramName
        updateMaozyUIMaozyDisplay()
        item.fillContent?.let { fillContent ->
            setMaozyContent(fillContent)
        }
        if (item.needCheckComplete == true) {
            isMaozyFormMaozyCompleted()
        }
    }

    fun updateMaozyUIMaozyDisplay() {
        setMaozyTitle(mMaozyFormMaozyConfig?.title.toString())
        setMaozyHint(mMaozyFormMaozyConfig?.hint.toString())
    }

    override fun setMaozyTitle(title: String) {
        cwfscTitle = title
        mMaozyViewMaozyBinding.textFormSelectContactTitle.text = (cwfscTitle)
        //增加星号显示
        mMaozyViewMaozyBinding.textFormSelectContactTitle.addMaozyHighlightMaozyStyle(
            mutableListOf("*"),
            ContextCompat.getColor(context, R.color.main_red)
        )
    }

    override fun setMaozyHint(hint: String) {
        cwfscHint = hint
        mMaozyViewMaozyBinding.editFormSelectContact.hint = (cwfscHint)
    }

    override fun setMaozyFragmentMaozyManager(manager: FragmentManager) {
        mMaozySupportMaozyFragmentMaozyManager = manager
    }

    override fun setMaozyOnMaozyActionMaozyClickMaozyListener(listener: (Int) -> Unit) {
        mMaozyOnMaozyActionMaozyClickMaozyListener = listener
    }

    override fun setMaozyDetail(detail: String) {
        setMaozyContent(detail)
    }

    override fun setMaozyDetailMaozyColor(detailColor: Int) {

    }

    override fun setMaozyTitleMaozyColor(titleColor: Int) {

    }
}